//#Author:bin
// -*- charset = utf-8 -*-
// @time:2021/12/18 16:13
// @Author:bfl91
// @file : address.js
// @Software:WebStorm
export default {
    namespaced:true,
    state:{
        addresList:[],//用户地址列表
        upobj:{},//显示用户修改的地址信息
    },
    getters:{
        list(state){
             return  state.addresList
        },
        Bool(state){
            let flag=true;
            state.addresList.forEach(v => {
                if(v.isDefault){
                    flag=false
                }
            })
            return {
                flag
            }
        },
        objaddres(state) {
            for(var i in state.addresList){
                if(state.addresList[i].isDefault){
                    return state.addresList[i]
                }
            }
        }
    },
    mutations:{
        //删除地址
        revomobj(state,index){
            state.addresList.splice(index, 1);
            localStorage.setItem('addresslist',JSON.stringify(state.addresList))
        },
        //修改的用户地址
        uplistobj(state,upaddress){
            let {obj,index}=upaddress;
            state.addresList.splice(index,1)
            state.addresList.forEach(v=>{
                if(obj.isDefault){
                    v.isDefault=false;
                }
            })
            state.addresList.splice(index,0,obj)
        },
        //显示要修改的用户地址
        updateList(state,index){
            state.upobj=state.addresList[index]
        },
        //添加用户地址
        seTaddList(state, obj) {
            //判断用户是否将地址设为默认值
            state.addresList.forEach((v,i)=>{
                if(obj.isDefault){
                    v.isDefault=false;
                }
            })
            state.addresList.push(obj);
            localStorage.setItem('addresslist',JSON.stringify(state.addresList))
        },
        //修改用户选中的默认地址
        seTaddress(state,obj) {
            let {bool,index}=obj
            state.addresList.forEach(v=>{
                v.isDefault=false
            })
                state.addresList[index].isDefault=bool
            }
    },
}